package de.g18.llmaa.gui.workspace.internalFrame.product.key;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import de.g18.llmaa.gui.App_LLMAA_Controler;
import de.g18.llmaa.gui.App_LLMAA_Model;
import de.g18.llmaa.gui.App_Model_Database;
import de.g18.llmaa.gui.IApp_Database_Model;
import de.g18.llmaa.gui.IApp_Model;
import de.g18.llmaa.gui.workspace.internalFrame.product.key.verleihVerwaltung.JPanel_KeyUser;
import de.g18.llmaa.logic.vos.VOKey;

public class JPanel_Key extends JPanel {

	private VOKey key;
	
	private JPanel jPanel_KeyInfo = null; // @jve:decl-index=0:visual-constraint="615,126"
	private JLabel jLabel = null;
	private JPanel_KeyUser panelKeyUser = null;
	private JTextField jTextField_KeyId = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField_KeyTyp = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField_ClaimedDate = null;
	private JLabel jLabel3 = null;
	private JTextField jTextField_Serial = null;

	public JPanel_Key(VOKey key) {
		this.key = key;
		initGUI();
		IApp_Model erg = App_LLMAA_Controler.getInstanze().getModel();
		
		if(erg instanceof App_Model_Database)
		{
			System.out.println("Datenbank");
		}
		if(erg instanceof App_LLMAA_Model)
		{
			System.out.println("xml");
		}
	}

	private void initGUI() {
		setLayout(new BorderLayout());
		add(getJPanel_KeyInfo(), BorderLayout.NORTH);
		
		if(App_LLMAA_Controler.getInstanze().getModel() instanceof IApp_Database_Model)
		{
		getPanelKeyUser().setBorder(new TitledBorder("Lizenz Nutzer"));
		
		add(getPanelKeyUser(),BorderLayout.CENTER);
		}

	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel_KeyInfo() {
		if (jPanel_KeyInfo == null) {
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints10.gridy = 4;
			gridBagConstraints10.weightx = 1.0;
			gridBagConstraints10.gridx = 1;

			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.gridy = 4;
			jLabel3 = new JLabel();
			jLabel3.setText("Serial");
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints8.gridy = 3;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 3;
			jLabel2 = new JLabel();
			jLabel2.setText("Datum:");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints5.gridy = 2;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 2;
			jLabel1 = new JLabel();
			jLabel1.setText("Key Typ:");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 1;
			jLabel = new JLabel();
			jLabel.setText("Key Id:");
			jPanel_KeyInfo = new JPanel();
			jPanel_KeyInfo.setLayout(new GridBagLayout());
			jPanel_KeyInfo.setSize(new Dimension(409, 114));
			jPanel_KeyInfo.add(jLabel, gridBagConstraints);
			jPanel_KeyInfo.add(getJTextField_KeyId(), gridBagConstraints3);
			jPanel_KeyInfo.add(jLabel1, gridBagConstraints4);
			jPanel_KeyInfo.add(getJTextField_KeyType(), gridBagConstraints5);
			jPanel_KeyInfo.add(jLabel2, gridBagConstraints6);
			jPanel_KeyInfo
					.add(getJTextField_ClaimedDate(), gridBagConstraints8);
			jPanel_KeyInfo.add(jLabel3, gridBagConstraints9);
			jPanel_KeyInfo.add(getJTextField_Serial(), gridBagConstraints10);
			
			jPanel_KeyInfo.setBorder(BorderFactory.createTitledBorder("Serial Information"));
			}
		return jPanel_KeyInfo;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField_KeyId() {
		if (jTextField_KeyId == null) {
			jTextField_KeyId = new JTextField();
			jTextField_KeyId.setPreferredSize(new Dimension(270,25));
			jTextField_KeyId.setText(key.getKey_Id()+"");
			jTextField_KeyId.setEditable(false);
		}
		return jTextField_KeyId;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField_KeyType() {
		if (jTextField_KeyTyp == null) {
			jTextField_KeyTyp = new JTextField();
			jTextField_KeyTyp.setPreferredSize(new Dimension(270,25));
			jTextField_KeyTyp.setText(key.getKey_Type().getType_Name());
			jTextField_KeyTyp.setEditable(false);
		}
		return jTextField_KeyTyp;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField_ClaimedDate() {
		if (jTextField_ClaimedDate == null) {
			jTextField_ClaimedDate = new JTextField();
			jTextField_ClaimedDate.setPreferredSize(new Dimension(270,25));
			if(key.getKey_ClaimedDate()!=null)
			{
				   Calendar cal = new GregorianCalendar( TimeZone.getTimeZone("ECT") );
				    SimpleDateFormat formater = new SimpleDateFormat();
				    formater.applyPattern( "EEEE dd. MMMM yyyy " );
//
			jTextField_ClaimedDate.setText(formater.format(key.getKey_ClaimedDate()));
			}
			jTextField_ClaimedDate.setEditable(false);
			
		}
		return jTextField_ClaimedDate;
	}
	

	public JPanel_KeyUser getPanelKeyUser() {
		if(panelKeyUser==null)
		{

			panelKeyUser = new JPanel_KeyUser(key);
		}
		return panelKeyUser;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField_Serial() {
		if (jTextField_Serial == null) {
			jTextField_Serial = new JTextField();
			jTextField_Serial.setPreferredSize(new Dimension(270,25));
			jTextField_Serial.setText(key.getKey_Serial());
			jTextField_Serial.setEditable(false);
		}
		return jTextField_Serial;
	}

}
